package com.ymatou.iapi.userservice.testcase;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONException;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.userservice.parameter.BatchUserRequestDtoBean;
import com.ymatou.iapi.userservice.parameter.req.CreateUserReq;
import com.ymatou.iapi.userservice.parameter.resp.UserInfo;
import com.ymatou.iapi.userservice.parameter.resp.UserInfo2;
import com.ymatou.iapi.userservice.service.BatchUserRequestDtoCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P2;
import com.ymt.utils.tag.P3;
import com.ymttest.business.service.JUserCallService;
import com.ymttest.database.model.Userinfo;
import com.ymttest.database.model.Userinfoex;
import com.ymttest.database.sqlwapper.JUserWapper;
import com.ymttest.utils.constant.ECode;

/**
 * 根据userid批量查询用户信息明细Test FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_BatchUserRequestDto {
	private static BatchUserRequestDtoBean batchuserrequestdtoBean;
	private static BatchUserRequestDtoCall batchuserrequestdtoCall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("根据userid批量查询用户信息明细Test");
	}

	@Before
	public void caseUp() {
		batchuserrequestdtoBean = new BatchUserRequestDtoBean();
		batchuserrequestdtoCall = new BatchUserRequestDtoCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(batchuserrequestdtoCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	private void verifyDB(Integer uid) throws JSONException {
		JUserWapper ju = new JUserWapper();
		ArrayList<UserInfo2> list = batchuserrequestdtoCall.getResult();

		for (UserInfo2 Result : list) {
			List<Userinfo> lui = ju.selectUserinfoByUid(Result.getUserId());
			Userinfo ui = lui.get(0);

			Logger.comment("验证Userinfo");
			Logger.verifyEquals(ui.getUserId(), Result.getUserId(), "验证userId");
			Logger.verifyEquals(ui.getLoginId(), Result.getLoginId(), "验证loginId");
			// 字段名返回错误
			Logger.verifyEquals(ui.getLoginMail(), Result.getLoginMail(), "验证LoginMail");
			Logger.verifyEquals(ui.getLoginMobile(), Result.getLoginMobile(), "验证loginMobile");
			// 数据库默认值问题
			Logger.verifyEquals(ui.getUserType(), Result.getUserType().getCode(), "验证userType");
			Logger.verifyEquals(ui.getUserState(), Result.getUserState(), "验证userState");
			Logger.verifyEquals(ui.getUserLevel().toString(), Result.getUserLevel(), "验证userlevel");
			Logger.verifyNotNull(Result.getCreateTime(), "验证CreateTime");
		}

//		ArrayList<Userinfoex> listex = batchuserrequestdtoCall.getResultex();
//		for (Userinfoex Result : listex) {
//			List<Userinfoex> lui2 = ju.selectUserinfoexByUid(Result.getUserId());
//			Userinfoex uiex = lui2.get(0);
//
//			Logger.comment("验证Userinfoex");
//			Logger.verifyEquals(uiex.getQQ(), Result.getQQ(), "验证qq");
//			Logger.verifyEquals(uiex.getLogoUrl(), Result.getLogoUrl(), "验证logUrl");
//
//		}

	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_BatchUserRequestDto_001() {
		Logger.start(true, "批量查询用户信息明细,成功");
		try {
			CreateUserReq cur = new CreateUserReq();
			cur = JUserCallService.initCreateUserBean(cur);
			Integer uid = JUserCallService.createUserCall(cur);
			String loginId = cur.getLoginId();
			List<Integer> userIdList = new ArrayList<>();
			userIdList.add(uid);
			batchuserrequestdtoBean.setUserIdList(userIdList);
			batchuserrequestdtoBean.setreqDomain("www.ymatou.com");
			batchuserrequestdtoCall.setData(batchuserrequestdtoBean);
			batchuserrequestdtoCall.callService();

			Logger.verifyEquals(ECode.SUCCESS, batchuserrequestdtoCall.getString("ResponseCode"), "验证ResponseCode");
			Logger.verifyEquals("true", batchuserrequestdtoCall.getString("Success"), "验证Success");

			verifyDB(uid);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P2.class })
	@TestCase
	public void Tc_BatchUserRequestDto_002() {
		Logger.start(true, "批量查询多个用户");
		try {
			List<Integer> userIdList = new ArrayList<>();
			List<String> loginIdList = new ArrayList<>();
			for (int i = 0; i < 3; i++) {
				CreateUserReq cur = new CreateUserReq();
				cur = JUserCallService.initCreateUserBean(cur);
				Integer uid = JUserCallService.createUserCall(cur);
				String loginId = cur.getLoginId();
				loginIdList.add(loginId);
				userIdList.add(uid);
			}

			batchuserrequestdtoBean.setUserIdList(userIdList);
			batchuserrequestdtoBean.setreqDomain("www.ymatou.com");
			batchuserrequestdtoCall.setData(batchuserrequestdtoBean);
			batchuserrequestdtoCall.callService();
			Logger.verifyEquals(ECode.SUCCESS, batchuserrequestdtoCall.getString("ResponseCode"), "验证ResponseCode");
			Logger.verifyEquals("true", batchuserrequestdtoCall.getString("Success"), "验证Success");

			verifyDB(userIdList.get(0));

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P3.class })
	@TestCase
	public void Tc_BatchUserRequestDto_003() {
		Logger.start(true, "不输入参数UserIdList，失败");
		try {
			CreateUserReq cur = new CreateUserReq();
			cur = JUserCallService.initCreateUserBean(cur);
			Integer uid = JUserCallService.createUserCall(cur);
			String loginId = cur.getLoginId();
			List<Integer> userIdList = new ArrayList<>();
			userIdList.add(uid);
			batchuserrequestdtoBean.setUserIdList(null);
			batchuserrequestdtoBean.setreqDomain("www.ymatou.com");
			batchuserrequestdtoCall.setData(batchuserrequestdtoBean);
			batchuserrequestdtoCall.callService();

			Logger.verifyEquals("400", batchuserrequestdtoCall.getString("ResponseCode"), "验证ResponseCode");
			Logger.verifyEquals("false", batchuserrequestdtoCall.getString("Success"), "验证Success");
			// Logger.verifyEquals("请求参数非法",
			// batchuserrequestdtoCall.getString("LastErrorMessage"),
			// "验证Message");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P3.class })
	@TestCase
	public void Tc_BatchUserRequestDto_004() {
		Logger.start(true, "输入参数不存在，失败");
		try {
			List<Integer> list = new ArrayList<>();
			list.add(-1000);
			batchuserrequestdtoBean.setUserIdList(list);
			// batchuserrequestdtoBean.setreqDomain(loginId);
			batchuserrequestdtoCall.setData(batchuserrequestdtoBean);
			batchuserrequestdtoCall.callService();

			Logger.verifyEquals("200", batchuserrequestdtoCall.getString("ResponseCode"), "验证ResponseCode");
			Logger.verifyEquals("true", batchuserrequestdtoCall.getString("Success"), "验证Success");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}